package router

import (
	"github.com/gin-gonic/gin"
	"monoblog/common"
	"monoblog/middleware"
	"monoblog/view"
	"monoblog/view/rest"
	"net/http"
)

// ViewRoute 页面请求路由
func ViewRoute(router *gin.RouterGroup) {
	//使用 gin.BasicAuth() 中间件
	admin := router.Group("/admin", gin.BasicAuth(common.GetAdminAccounts()), middleware.AccessView)
	{
		admin.GET("/consolelog", view.ConsoleLogPage)
		admin.GET("/accesslog", view.AccessLogPage)
	}

	//可以传入多个全局中间件
	router = router.Group("/", middleware.AccessView, middleware.CookieSetLoginInfo)

	router.GET("/", func(ctx *gin.Context) {
		ctx.Redirect(http.StatusMovedPermanently, "/index")
	})
	router.GET("/demo", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "demo.tmpl", gin.H{})
	})

	router.GET("/login", view.Login)
	router.GET("/register", view.Register)
	router.GET("/index", view.Index)
	router.GET("/search", view.Search)
	router.GET("/category", view.Category)
	router.GET("/category/:cateId", view.Category)
	router.GET("/section/:sectionId", view.Section)
	router.GET("/blog/:id", view.BlogDetail)
	router.GET("/blog/preview/:id", view.BlogPreview)

	router.GET("/user/:userId", view.UserProfile)
	router.GET("/settings", view.Settings)
	router.GET("/writing", view.WritingBoard)
	router.GET("/writing/edit/:id", view.WritingEdit)
	router.GET("/writing/manage", view.WritingManage)
	router.GET("/writing/section", view.WritingSection)
	router.GET("/writing/analysis", view.WritingAnalysis)

	router.GET("/feedback", view.Feedback)

}

// Route 接口请求路由
func Route(router *gin.RouterGroup) {
	router = router.Group("/rest")
	//可以传入多个全局中间件
	router.Use(middleware.AccessLimit, middleware.AccessMonitor, middleware.CookieSetLoginInfo, middleware.AuthSetLoginInfo, middleware.BusinessLimit)

	{
		adminRouter := router.Group("/admin")
		adminRouter.GET("/access-log", rest.AccessLog)
		adminRouter.GET("/stream-console", rest.StreamConsole)
		adminRouter.GET("/download-log", rest.LogFileDownload)
		adminRouter.POST("/push-blog", rest.ManualPushBlog)
	}

	{
		authRouter := router.Group("/auth")
		authRouter.POST("/login", rest.AuthLogin)
		authRouter.POST("/register", rest.RegisterUser)
		authRouter.POST("/logout", rest.AuthLogout)
		authRouter.GET("/check-logged", rest.CheckIsLogged)
	}

	router.GET("/occupied", rest.CheckOccupied)
	router.GET("/smscode", rest.SendSmsCode)

	router.GET("/query/:userId", rest.QueryData)
	router.POST("/form/:userId", rest.FormSubmit)
	router.POST("/body/:userId", rest.BodySubmit)

	/*分类接口*/

	router.GET("/category/list", rest.CategoryFindList)
	router.POST("/category/add", rest.CategoryAdd)
	router.PUT("/category/:id", rest.CategoryUpdate)
	router.DELETE("/category/:id", rest.CategoryDelete)

	/*专栏接口*/

	router.GET("/section/list", rest.SectionFindList)
	router.POST("/section/add", rest.SectionAdd)
	router.PUT("/section/:id", rest.SectionUpdate)
	router.DELETE("/section/:id", rest.SectionDelete)

	/*文章接口*/

	router.POST("/article/save", rest.ArticleSave)
	router.POST("/article/save/:id", rest.ArticleSave)
	router.PUT("/article/:id/publish", rest.ArticlePublish)
	router.DELETE("/article/:id", rest.ArticleDelete)

	router.GET("/article/list/user", rest.ArticleListByUser) // 查询登录用户的文章列表
	router.GET("/article/list/section", rest.BlogsBySection) // 查询专栏的文章列表

	router.GET("/article/:id", rest.ArticleFindById)
	router.GET("/article/:id/read", rest.ArticleReadById)

	/*文章点赞，收藏*/
	router.POST("/article/:id/favorite", rest.FavoriteTheBlog)              // 文章收藏
	router.POST("/article/:id/cancel-favorite", rest.CancelFavoriteTheBlog) // 取消文章收藏
	router.POST("/article/:id/like", rest.LikeTheBlog)                      // 文章点赞
	router.POST("/article/:id/cancel-like", rest.CancelLikeTheBlog)         // 取消文章点赞
	router.POST("/comment/:id/like", rest.LikeTheComment)                   // 评论点赞
	router.POST("/comment/:id/cancel-like", rest.CancelLikeTheComment)      // 取消评论点赞

	router.POST("/user/:id/follow", rest.UserToFollow)        // 用户关注
	router.POST("/user/:id/cancel-follow", rest.CancelFollow) // 用户取消关注

	/*反馈接口*/
	router.POST("/feedback/add", rest.SubmitFeedback)   // 提交反馈
	router.GET("/feedback/my", rest.GetFeedbacksByUser) // 查看用户的历史反馈列表

	router.POST("/user/:id/black", rest.UserToBlack)            // 用户拉黑
	router.POST("/user/:id/cancel-black", rest.UserCancelBlack) // 用户取消拉黑

	/*用户相关接口*/
	router.POST("/user/add", rest.RegisterUser)
	router.GET("/user/one/:userId", rest.FindUserByUserID)
	router.GET("/user/detail/:userId", rest.FindUserDetail)
	router.GET("/user/page", rest.FindUserPage)

	/*评论相关接口*/
	router.GET("/article/:id/comments", rest.CommentsOfBlog) //查看文章评论列表
	router.POST("/comment/add", rest.SubmitComment)          //发表评论

	router.GET("/comment/my", rest.CommentsFromMy) //查看我的评论列表

	router.GET("/popular-blogs", rest.PopularBlogs)

	/*状态相关*/
	router.GET("/stats/blog", rest.StatsAtBlog)
	router.GET("/stats/user", rest.StatsAtUser)

	/*个人中心相关接口*/
	router.GET("/me/pub-articles", rest.PubBlogsByUser) // 用户文章列表
	router.GET("/me/sections", rest.SectionsByUser)     // 用户专栏列表
	router.GET("/me/favorites", rest.FavoritesByUser)   // 用户收藏列表（收藏的文章）
	router.GET("/me/follows", rest.FollowsByUser)       // 用户关注列表（关注的用户）

	router.GET("/me/moments", rest.MomentsByUser)   // 用户动态列表
	router.GET("/me/settings", rest.SettingsByUser) // 用户的设置

	router.GET("/me/notifications", rest.NotificationsByUser) // 系统通知列表

	/*图片文件上传接口*/
	router.POST("/upload/image", rest.PictureUpload)

}
